package com.ym.student.system.controller;

import com.ym.student.system.entity.SysRole;
import com.ym.student.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/roles")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    // 获取所有角色
    @GetMapping
    public List<SysRole> getAllRoles() {
        return sysRoleService.list();
    }

    // 根据ID获取角色
    @GetMapping("/{id}")
    public SysRole getRoleById(@PathVariable("id") Integer roleId) {
        return sysRoleService.getById(roleId);
    }

    // 添加角色
    @PostMapping
    public boolean addRole(@RequestBody SysRole sysRole) {
        return sysRoleService.save(sysRole);
    }

    // 更新角色
    @PutMapping("/{id}")
    public boolean updateRole(@PathVariable("id") Integer roleId, @RequestBody SysRole sysRole) {
        sysRole.setRoleId(roleId); // 确保更新的是指定ID的角色
        return sysRoleService.updateById(sysRole);
    }

    // 删除角色
    @DeleteMapping("/{id}")
    public boolean deleteRole(@PathVariable("id") Integer roleId) {
        return sysRoleService.removeById(roleId);
    }
}